package com.tencent.cloud.iov.flow.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;

/**
 * 视图容器默认实现类
 *
 * @author showxiao
 * @date 2018/6/19
 */

public class FlowViewContainer implements IContentViewContainer {

    private final ViewGroup mContainer;

    public FlowViewContainer(@NonNull ViewGroup container) {
        mContainer = container;
    }

    @Override
    public void setContentView(int resId) {
        setContentView(LayoutInflater.from(mContainer.getContext())
                .inflate(resId, mContainer, false)
        );
    }

    @Override
    public void setContentView(View contentView) {
        setContentView(contentView, contentView.getLayoutParams());
    }

    @Override
    public void setContentView(View contentView, ViewGroup.LayoutParams params) {
        if (params == null) {
            params = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        contentView.setLayoutParams(params);

        mContainer.removeAllViews();
        mContainer.addView(contentView);
    }

    @Override
    public Context getContext() {
        return mContainer.getContext();
    }
}
